package net.pws.common.persistence.hibernate3.utils;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.Map;

public abstract class JdbcUtils {
    
    public static void setPreparedStatementParameters(final PreparedStatement ps,
                                                      final int startParameterIndex,
                                                      final Map namedParameters) throws SQLException {
        int i = startParameterIndex;
        for (Iterator iterator = namedParameters.values().iterator(); iterator.hasNext(); i++) {
            Object value = iterator.next();
            if (null == value) {
                ps.setString(i, null);
            }
            else if (value instanceof String) {
                ps.setString(i, (String) value);
            }
            else if (value instanceof Date) {
                ps.setDate(i, (Date) value);
            }
            else if (value instanceof Timestamp) {
                ps.setTimestamp(i, (Timestamp) value);
            }
            else {
                ps.setString(i, value.toString());
            }
        }
    }
}
